package cn.orangelite.ai.controller;

import cn.orangelite.ai.domain.AiGptKey;
import cn.orangelite.ai.domain.dto.ChatDTO;
import cn.orangelite.ai.service.IAiGptKeyService;
import cn.orangelite.common.core.utils.poi.ExcelUtil;
import cn.orangelite.common.core.web.controller.BaseController;
import cn.orangelite.common.core.web.domain.AjaxResult;
import cn.orangelite.common.core.web.page.TableDataInfo;
import cn.orangelite.common.log.annotation.Log;
import cn.orangelite.common.log.enums.BusinessType;
import cn.orangelite.common.redis.service.RedisService;
import cn.orangelite.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * gptkey
 Controller
 *
 * @author dongfaqing
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/gpt")
public class AiGptKeyController extends BaseController
{
    @Autowired
    private IAiGptKeyService aiGptKeyService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询gptkey
     列表
     */
    @RequiresPermissions("ai:gpt:list")
    @GetMapping("/list")
    public TableDataInfo list(AiGptKey aiGptKey)
    {
        startPage();
        List<AiGptKey> list = aiGptKeyService.selectAiGptKeyList(aiGptKey);
        return getDataTable(list);
    }

    /**
     * 导出gptkey
     列表
     */
    @RequiresPermissions("ai:gpt:export")
    @Log(title = "gptkey", businessType = BusinessType.EXPORT)
            @PostMapping("/export")
            public void export(HttpServletResponse response, AiGptKey aiGptKey)
    {
        List<AiGptKey> list = aiGptKeyService.selectAiGptKeyList(aiGptKey);
        ExcelUtil<AiGptKey> util = new ExcelUtil<AiGptKey>(AiGptKey.class);
        util.exportExcel(response, list, "gptkey数据");
    }

    /**
     * 获取gptkey
     详细信息
     */
    @RequiresPermissions("ai:gpt:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aiGptKeyService.selectAiGptKeyById(id));
    }

    /**
     * 新增gptkey

     */
    @RequiresPermissions("ai:gpt:add")
    @Log(title = "gptkey", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiGptKey aiGptKey){
        return toAjax(aiGptKeyService.insertAiGptKey(aiGptKey));
    }

    /**
     * 修改gptkey

    */
    @RequiresPermissions("ai:gpt:edit")
    @Log(title = "gptkey", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiGptKey aiGptKey){
        return toAjax(aiGptKeyService.updateAiGptKey(aiGptKey));
    }

    /**
     * 删除gptkey

    */
    @RequiresPermissions("ai:gpt:remove")
    @Log(title = "gptkey", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aiGptKeyService.deleteAiGptKeyByIds(ids));
    }


    @PostMapping("/chat")
    public AjaxResult chat(@RequestBody ChatDTO chatDTO) {
        try{
            aiGptKeyService.gptChat(chatDTO);
            return success("成功");
        }catch (Exception e){
            return error("请求失败");
        }
    }
}
